package com.pxmeta.skill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.pxmeta.skill.entity.Skill;
import com.pxmeta.party.entity.vo.SkillCategoryVO;
import com.pxmeta.skill.mapper.SkillMapper;
import com.pxmeta.skill.service.ISkillService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class SkillServiceImpl extends ServiceImpl<SkillMapper, Skill> implements ISkillService {


    @Override
    public List<Skill> getHotSkillList() {
        LambdaQueryWrapper<Skill> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Skill::getIsHot, Boolean.TRUE)
                .orderByAsc(Skill::getSort);
        return this.list(wrapper);
    }

    @Override
    public List<SkillCategoryVO> getAllSkillList() {

        List<Skill> list = this.list();
        List<Skill> hotSkillList = this.getHotSkillList();

        List<SkillCategoryVO> categoryVOS = Lists.newLinkedList();
        SkillCategoryVO hotVo = new SkillCategoryVO();
        hotVo.setCategoryName("热门");
        hotVo.setSkillList(hotSkillList);
        hotVo.setCategoryId(0);
        categoryVOS.add(hotVo);

        Map<Integer, List<Skill>> map = list.stream().collect(Collectors.groupingBy(Skill::getCategory));
        for (Map.Entry<Integer, List<Skill>> entry : map.entrySet()) {
            SkillCategoryVO vo = new SkillCategoryVO();
            Integer key = entry.getKey();
            vo.setCategoryId(key);
            vo.setSkillList(entry.getValue());
            if (key == 1){
                vo.setCategoryName("娱乐");
            }
            if (key == 2){
                vo.setCategoryName("游戏");
            }
            if (key == 3){
                vo.setCategoryName("职业");
            }

            categoryVOS.add(vo);
        }

        return categoryVOS;

    }

    @Override
    public List<Skill> getSkillListByCategory(Integer category, Long userId) {
        return baseMapper.getSkillListByCategory(category, userId);
    }
}
